﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using DataEntity;
using JScripter;
using BusinessLogic.Controller;

public partial class Admin_AjaxPages_PropertyTypeControlSettingAdd : AjaxPage
{
    private Guid ID
    {
        get
        {
            if (Request.Params["ptcsid"] != null || AjaxState.ContainsKey("ptcsid"))
            {
                if (!AjaxState.ContainsKey("ptcsid"))
                    AjaxState["ptcsid"] = Request.Params["ptcsid"];
                return new Guid(AjaxState["ptcsid"]);
            }
            else
            {
                return Guid.Empty;
            }
        }
    }


    private void AddData()
    {
        try
        {
            PropertyTypeControlSetting objPropertyTypeControlSetting = new PropertyTypeControlSetting();

            objPropertyTypeControlSetting.PropertyTypeControlSettingID = Guid.NewGuid();

            objPropertyTypeControlSetting.PropertyTypeID = Convert.ToInt32(HtmlHelper.ControlValue(ddPropertyTypeID.ClientID));

            objPropertyTypeControlSetting.ControlClientID = HtmlHelper.ControlValue(txtControlClientID.ClientID);

            objPropertyTypeControlSetting.IsVisible = Convert.ToBoolean(HtmlHelper.CheckBoxControlValue(chkIsVisible.ClientID));

            objPropertyTypeControlSetting.ModifiedDate = DateTime.Now;

            new PropertyTypeControlSettingController().Add(objPropertyTypeControlSetting);
            Response.Redirect("~/Admin/AjaxPages/PropertyTypeControlSettingList.aspx");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = "<div class='error'>" + ex.Message + "</div>";
        }
    }

    private void EditData()
    {

        PropertyTypeControlSetting objPropertyTypeControlSetting = new PropertyTypeControlSetting();

        objPropertyTypeControlSetting.PropertyTypeControlSettingID = ID;

        objPropertyTypeControlSetting.PropertyTypeID = Convert.ToInt32(HtmlHelper.ControlValue(ddPropertyTypeID.ClientID));

        objPropertyTypeControlSetting.ControlClientID = HtmlHelper.ControlValue(txtControlClientID.ClientID);

        objPropertyTypeControlSetting.IsVisible = Convert.ToBoolean(HtmlHelper.CheckBoxControlValue(chkIsVisible.ClientID));

        objPropertyTypeControlSetting.ModifiedDate = DateTime.Now;

        new PropertyTypeControlSettingController().UpdateByPropertyTypeControlSettingID(objPropertyTypeControlSetting);
        Response.Redirect("~/Admin/AjaxPages/PropertyTypeControlSettingList.aspx");
    }

    private void BindData()
    {
        var dataBunch = new PropertyTypeControlSettingController().GetbyPropertyTypeControlSettingID(ID);
        if (dataBunch.Count > 0)
        {
            var data = dataBunch[0];

            new PropertyTypeController().BindPropertyType(ddPropertyTypeID, data.PropertyTypeID.ToString());
            

            txtControlClientID.Text = data.ControlClientID;

            chkIsVisible.Checked = data.IsVisible;

            

        }
    }

    protected void AddAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        AddData();

    }
    protected void UpdateAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        EditData();
    }
    public void PreventData()
    {

        txtControlClientID.Text = HtmlHelper.ControlValue(txtControlClientID.ClientID);
        chkIsVisible.Checked = Convert.ToBoolean(HtmlHelper.CheckBoxControlValue(chkIsVisible.ClientID));
        

    }

    protected override void OnLoad(EventArgs e)
    {
        if (ID != Guid.Empty)
        {
            BindData();
        }
        else
        {
            new PropertyTypeController().BindPropertyType(ddPropertyTypeID);
        }
        base.OnLoad(e);
    }

}